﻿using System;
using Component.Track;
using Component.UI.View;
using FScrollPage;
using TMPro;
using UnityEngine;

namespace Component.UI.Entity
{
    public class StageView : MonoBehaviour
    {
        private int stageId;
        [SerializeField] private TextMeshProUGUI nameText;
        [SerializeField] private TextMeshProUGUI describe;
        [SerializeField] private TextMeshProUGUI score;

        [SerializeField] private StationManager target;
        [SerializeField] private GameObject[] visible;
        [SerializeField] private Transform selectViewPivot;
        [SerializeField] private Transform challengeParent;
        
        private StageItem item;

        private GameObject challengeModel;
        public Transform TargetTransform => target.transform;
        public StationManager Target => target;

        private void Awake()
        {
            challengeModel = Resources.Load<GameObject>("Prefabs/Challenge");
            item = GetComponent<StageItem>();
            item.click = OnScrollClick;
            item.pitch = OnScrollPitch;
            item.over = OnScrollOver;
        }

        private void Start()
        {
        }

        private bool Contains(int id)
        {
            return stageId == id;
        }
        
        public void InitView(StageEntity stage)
        {
            stageId = stage.StageId;
            nameText.text = stage.StageName;
            describe.text = stage.StageDescribe;
            score.text = stage.Score.ToString();

            //获取关卡卡片并初始化
            GameObject go = Instantiate(challengeModel, challengeParent);
            item.Child = go.GetComponent<ChallengeItem>();
            go.GetComponent<ChallengeView>().InitView(stage.Challenges);

        }
        private void OnScrollPitch()
        {
            foreach (var go in visible)
            {
                go.SetActive(true);
            }

            GameManager.Instance.OnStageSelected(stageId);
        }        
        private void OnScrollOver()
        {
            foreach (var go in visible)
            {
                go.SetActive(false);
            }
        }
        private void OnScrollClick()
        {
            GameManager.Instance.StartGame();
        }
    }

}